package com.cn.medicalcare.service;

import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.AdminMapper;
import com.cn.medicalcare.pojo.Admin;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.List;

public class AdminService {
    private static final String PREFIX="pOiIVciO";
    private static final String SUFFIX="Nlvo";


    //静态内部类单例模式
    private static class SingletonClassInstance{
        private static final AdminService instance=new AdminService();
    }

    private AdminService(){}

    public static AdminService getInstance(){
        return SingletonClassInstance.instance;
    }


    public Admin selectAdminByUsername(String username){
        return SqlSessionFactoryTool.DoMapper(AdminMapper.class, m->m.selectAdminByName(username));
    }


    public boolean insertAdmin(Admin admin){
        admin.setPassword(encryption(admin.getUsername()));
        return SqlSessionFactoryTool.DoMapperAndCommit(AdminMapper.class, m->m.insertAdmin(admin));
    }

    public boolean updataAdmin(Admin admin) {
        return SqlSessionFactoryTool.DoMapperAndCommit(AdminMapper.class, m->m.updataAdmin(admin));
    }


    public boolean updataAdminPassword(Admin admin){
        admin.setPassword(encryption(admin.getPassword()));
        return SqlSessionFactoryTool.DoMapperAndCommit(AdminMapper.class, m->m.updataAdminPassword(admin));
    }

    /**
     * MD5 加密
     * @param password   原始密码
     * @return      加密后的密码
     */
    public String encryption(String password){
        return DigestUtils.md5Hex(AdminService.PREFIX+password+ AdminService.SUFFIX);
    }
}
